//
//  taskCollection.swift
//  demo01
//
//  Created by babybus on 2025/7/1.
//
import Foundation
import UIKit
import SnapKit
import Alamofire

class TaskCollection:UICollectionView,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    //全局变量
    private var taskItems:[TaskInof]=[]
    var finalTime:String = ""
    private let api = "/tasklist"
    //初始化方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        attributeInit()
    }
    convenience init(frame: CGRect) {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        self.init(frame: frame, collectionViewLayout: layout)
        loadData(userId: 1)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //属性配置
    func attributeInit(){
        backgroundColor = .white
        layer.cornerRadius = 15
        
        dataSource = self
        delegate = self
        register(TaskCell.self, forCellWithReuseIdentifier: "TaskCell")
        register(TaskHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "taskHeader")
        register(TaskFooterView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "taskFooter")
        
    }
    //布局配置
    func setupUI(){
        
    }
    func loadData(userId:Int){
        let url = AllUrl.LH_URL + api + "?userId=\(userId)"
        AF.request(url,method: .get)
            .validate(statusCode: 200..<300)
            .responseDecodable(of:TaskList.self){ response in
                switch response.result{
                case .success(let taskList):
                    DispatchQueue.main.async {
                        self.taskItems = taskList.cellContent
                        self.finalTime = taskList.finalTime
                        self.reloadData()
                    }
                case .failure(let error):
                    print("错误: \(error.localizedDescription)")
                }
            }
    }
    
    //collection函数
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return taskItems.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TaskCell", for: indexPath)as! TaskCell
            let  item = taskItems[indexPath.row]
            cell.configure(image: UIImage(systemName: item.imageOfTask)!, nameOfTask: item.nameOfTask, detailOfTask: item.detailOfTask, numOfButton: item.numOfButton)
            self.reloadData()
            return cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader{
            let header = collectionView.dequeueReusableSupplementaryView(
                ofKind: kind,
                withReuseIdentifier: "taskHeader",
                for: indexPath)as! TaskHeaderView
            header.finalTime = self.finalTime
            return header
        }else if kind == UICollectionView.elementKindSectionFooter{
            let footer = collectionView.dequeueReusableSupplementaryView(
                ofKind: kind,
                withReuseIdentifier: "taskFooter",
                for: indexPath)as! TaskFooterView
            return footer
        }
        return UICollectionReusableView()
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

//            let width = (collectionView.bounds.width-20)
        return CGSize(width: 307, height: 52);
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.bounds.width, height: 64.75)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.bounds.width, height: 41)
    }
    
}
